#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === File Uploader Mega.nz ===
function uploadmega()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: uploadmega <file_path> [target_folder] (Upload to MEGA.nz)';
    echo '';
    return;
  fi;

  # Verify megatools is installed
  if ! type megals >/dev/null 2>&1; then
    echo '';
    echo ' megatools: Please install megatools before using tools like "megals"';
    echo '';
    return;
  fi;

  # Ignore missing credentials
  if [ ! -f "${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" ]; then
    echo '';
    echo " megatools: ${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc is missing, please read megatools documentation";
    echo '';
    return;
  fi;

  # Variables
  local path=${1};
  local target=${2:-Development};
  local time_start;
  local time_span;

  # Initialize variables
  time_start=$(date +%s);

  # Load path from PACKAGE_RESULT
  if [ -z "${path}" ] && [ -f "${PACKAGE_RESULT}" ]; then
    path=${PACKAGE_RESULT};
  fi;

  # File full path
  if [ -f "${path}" ]; then
    path=$(readlink -f "${path}");
  fi;

  # Detect main target inclusion
  if [[ ! "${target}" == *'/Root/'* ]]; then
    target=/Root/Android/${target};
  fi;

  # Upload header informations
  echo '';
  echo -e " \e[1;33m[ Uploading to the server - Path '${target}' ]\e[0m";

  # Use AndroidFiles to save the file
  if [ ! -z "${ANDROID_FILES_PATH}" ] && [[ "${target}" == *'Root/Android'* ]]; then
    androidfilesadd "${path}" "${target#/Root/Android/}";
  fi;

  # Remove local megatools cache
  rm -rf /tmp/*.megatools.cache;

  # Upload file to target
  echo '';
  megarm --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
      "${target}/$(basename "${path}")" 2> /dev/null;
  megamkdir --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
      "${target}" 2> /dev/null;
  megaput --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
      --path "${target}" "${path}";

  # Reload mega.nz account
  meganzreload;

  # Show upload time
  time_span=$(($(date +%s) - time_start));
  echo -e " \e[1;37m[ Upload done in ${time_span} secs ]\e[0m";
  echo '';
}

# === File Downloader Mega.nz ===
function downloadmega()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: downloadmega <remote_path> [boot_remove_remote] (Download from MEGA.nz)';
    echo '';
    return;
  fi;

  # Verify megatools is installed
  if ! type megals >/dev/null 2>&1; then
    echo '';
    echo ' megatools: Please install megatools before using tools like "megals"';
    echo '';
    return;
  fi;

  # Ignore missing credentials
  if [ ! -f "${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" ]; then
    echo '';
    echo " megatools: ${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc is missing, please read megatools documentation";
    echo '';
    return;
  fi;

  # Variables
  local path=${1};
  local remove_remote=${2};
  local file_name;
  local time_start;
  local time_span;

  # Initialize variables
  file_name=$(basename "${path}");
  time_start=$(date +%s);

  # Detect main target inclusion
  if [[ ! "${path}" == *'/Root/'* ]]; then
    path=/Root/Android/${path};
  fi;

  # Upload header informations
  echo '';
  echo -e " \e[1;33m[ Download from the server - Path '${path#/Root/Android/}' ]\e[0m";

  # Remove local megatools cache
  rm -rf /tmp/*.megatools.cache;

  # Save local file
  if [ -f "${file_name}" ]; then
    rm -f "${file_name}.tmp";
    mv "${file_name}" "${file_name}.tmp";
  fi;

  # Download file from target
  echo '';
  megaget --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
      --path ./ "${path}";

  # Delete remote file
  if [ "${remove_remote}" = 'true' ] && [ -f "${file_name}" ]; then
    megarm --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
        "${path}";
  fi;

  # Clean saved file
  if [ -f "${file_name}" ]; then
    rm -f "${file_name}.tmp";

  # Warn about failed download
  else
    echo '';
    echo -e " \e[1;31m[ Download failed ]\e[0m";
    echo '';
    return;
  fi;

  # Show download time
  echo '';
  time_span=$(($(date +%s) - time_start));
  echo -e " \e[1;37m[ Download done in ${time_span} secs ]\e[0m";
  echo '';
}

# === Private Uploader Mega.nz ===
function uploadprivatemega()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: uploadprivatemega <device_name> (Upload to private MEGA.nz)';
    echo '';
    return;
  fi;

  # File upload to Private
  uploadmega "${1}" '/Root/Private';
}

# === Folder Syncer Mega.nz ===
alias syncmega='megamirror';

# === Files Lister Mega.nz ===
function listmega()
{
  # Usage: listmega [options] (List remote files on MEGA.nz)';

  # Verify megatools is installed
  if ! type megals >/dev/null 2>&1; then
    echo '';
    echo ' megatools: Please install megatools before using tools like "megals"';
    echo '';
    return;
  fi;

  # Ignore missing credentials
  if [ ! -f "${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" ]; then
    echo '';
    echo " megatools: ${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc is missing, please read megatools documentation";
    echo '';
    return;
  fi;

  megals --config="${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.megarc" \
      "${@}";
}
